<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Gongjiangrenxin extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'order_no' =>  $bean->getOrderNo(), //订单号
            'business_number' =>  $bean->getSupplierNum(), //商户密钥
            'good_id' => $bean->getGoodsId(), //商品编码
            'account' => $bean->getAccount(),
            'num' => (string)($bean->getRechargeNum() * $bean->getStartCharging()),
            'ip' => request() -> ip(),//获取用户当前的ip,
            'notify_url' =>  $bean->getNotifyUrl() //异步回调地址
        ];
        ksort($array);
        $array['sign'] = md5(json_encode($array) . $bean->getSecretKey());
        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('state', '');
        $order_sn = $request->input('other_order_no', '');
        return $this
            ->apiNotifyData($order_sn, $status)
            ->setExecute(in_array($status, ['2', '3']))
            ->setRechargeSuccess($status == '2');
    }

}
